import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

import java.awt.event.*;

import javax.swing.*;

public class TownPanel extends JPanel implements KeyListener {
	
	private TownLocation[][] town = new TownLocation[5][7];
	private Point pLocation;
	private NewGame game;
	/**
	 * 0[  0  ][ 1  ][ 2  ][ 3  ][  4  ][ 5  ][      6     ]
	 * 1[Store][    ][Pub ][    ][Assay][    ][Land_Offices]
	 * 2[Road ][    ][Road][    ][Road ][    ][Road        ]
	 * 3[Road ][Road][Road][Road][Road ][Road][Road        ]
	 * 4[     ][    ][    ][Road][     ][    ][            ]
	 * 
	 * main town panel contains all town panel information
	 * @param p players
	 */
	public TownPanel(Player p, NewGame g) {
		game = g;
		addKeyListener(this);
		setPreferredSize(new Dimension(700, 500));
		setFocusable(true);
		setVisible(true);
		validate();
		pLocation = new Point(32, 32);
		town[1][0] = new Store(new Point(1, 0));
		town[1][2] = new Pub(new Point(1, 2));
		town[1][4] = new Assay(new Point(1, 4));
		town[1][6] = new LandOffices(new Point(1, 6));
		
		town[2][0] = new Road(new Point(2, 0));
		town[2][2] = new Road(new Point(2, 2));
		town[2][4] = new Road(new Point(2, 4));
		town[2][6] = new Road(new Point(2, 6));

		for (int i = 0; i < 7; i++) {
			town[3][i] = new Road(new Point(3,i));
		}
		town[4][3] = new Road(new Point(4,3));
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 5; j++) {
				if (town[j][i] == null) {
					town[j][i] = new Grass(new Point(j, i));
				}
			}
		
		}
	}
	
	/**
	 * Painting the whole town panel
	 * @param g graphic
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		for(int i = 0; i < 5; i ++){
			for(int j = 0; j < 7; j++){
				town[i][j].draw(g, 100, 100);
		    }
	    }
		TownPlayer p = new TownPlayer(pLocation);
		g.drawImage(p.i, (int)pLocation.getX()*10, (int)pLocation.getY()*10, 50, 50, null);
	}
	
	/**
	 * Setting the player reach town point
	 * @param p player
	 */
	public void reachTown(Player p) {
		p.setTownLocation(new Point(3,4));
	}
	/**
	 * Checking the location of player
	 * 0 = road
	 * 1 = store
	 * 2 = pub
	 * 3 = assay
	 * 4 = landoffices
	 * -1 = road
	 * 
	 */
	public int checkLocation() {
		int x = (int)pLocation.getX(), y = (int)pLocation.getY();
		if (town[y][x] == new Store(new Point(y, x))) {
			return 1;
		}
		else if (town[y][x] == new Pub(new Point(y, x))) {
			return 2;
		}
		else if (town[y][x] == new Assay(new Point(y, x))) {
			return 3;
		}
		else if (town[y][x] == new LandOffices(new Point(y, x))) {
			return 4;
		}
		else if (town[y][x] == new Road(new Point(y,x))) {
			return 0;
		}
		else return -1;
	}
	
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

		public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

		public void keyTyped(KeyEvent e) {
			System.out.println("KeyTyped");
			System.out.println("pLocation Y: " + pLocation.getY());
			System.out.println("pLocation X: " + pLocation.getX());

		if (e.getKeyChar() == 'w' && pLocation.getY() > 0) {
			pLocation.setLocation(pLocation.getX(), pLocation.getY()-1);
			System.out.println("UP");
		}
		else if (e.getKeyChar() == 's' && pLocation.getY() < 45) {
			pLocation.setLocation(pLocation.getX(), pLocation.getY()+1);
			System.out.println("DOWN");
		}
		else if (e.getKeyChar() == 'd' && pLocation.getX() < 65) {
			pLocation.setLocation(pLocation.getX()+1, pLocation.getY());
			System.out.println("RIGHT");
		}
		else if (e.getKeyChar() == 'a' && pLocation.getX() > 0) {
			pLocation.setLocation(pLocation.getX()-1, pLocation.getY());
			System.out.println("LEFT");
		}
		if (pLocation.getY() == 45) {
			if (pLocation.getX() > 29 && pLocation.getX() < 37) {
				game.playerOutTown();
			}
		}
		repaint();
	}
		
}
